-- yi-utils.lua
-- Displays useful RAM addresses for Yoshi's Island.
-- by bahamete

-- Config 
local doShowInfo = true
local doShowCounters = true
local doShowTimers = true
local doShowYoshiInfo = true
local guiTransparency = 1

-- Emu-Specific
if not emu then error("This script runs under Snes9x.") end

-- RAM
local XSUBPOS         = 0x70008a 
local XPOS            = 0x70008c
local YPOS            = 0x700090
local XSPEED          = 0x7000a8
local YSPEED          = 0x7000aa
local XSPEEDNEXT      = 0x7000b4
local DIRECTION       = 0x7000c4 -- 02 = left, 00 = right
local EATTIMER        = 0x7001ee
local TRANSFORMTIMER  = 0x7001f4
local STARTIMER       = 0x701e04
local SWITCHTIMER     = 0x701e08 -- ! Switch
local REDCOINCOUNTER  = 0x7e03b4
local STARSCOUNTER    = 0x7e03b6 -- * 10
local FLOWERSCOUNTER  = 0x7e03b8
local COINSCOUNTER = 0x7e037b
 
-- Script functions
local displayYoshiInfo = function()
  local x   = memory.readword(XPOS)
  local xsub= memory.readbyte(XSUBPOS)
  local y   = memory.readword(YPOS)
  local xs  = memory.readwordsigned(XSPEED)
  local ys  = memory.readwordsigned(YSPEED)
  local xsn = memory.readwordsigned(XSPEEDNEXT)
  local dir = memory.readbyte(DIRECTION)
  local eat = memory.readword(EATTIMER)

  local dirFormat = string.format("DIR %d", dir)
  local posFormat = string.format("POS %d.%03d, %d", x, xsub, y)
  local spdFormat = string.format("SPD %+04d (%03d), %d", xs, xsn, ys)  
  local eatFormat = string.format("EAT %d", eat)

  gui.text(1, 78, dirFormat )
  gui.text(1, 62, posFormat )
  gui.text(1, 70, spdFormat )
  --if eat > 0 then gui.text(2, 144, eatFormat) end
end

local displayTimers = function()
  local transform = memory.readword(TRANSFORMTIMER)
  local switch    = memory.readword(SWITCHTIMER)
  local star      = memory.readword(STARTIMER)

  local s_transform = string.format("FORM %d", transform)
  local s_switch = string.format("SWITCH %d", switch)
  local s_star = string.format("STAR %d", star)

  if transform > 0 then gui.text(1, 94, s_transform) end
  if switch > 0 then gui.text(1, 102, s_switch) end
  if star > 0 then gui.text(1, 110, s_star) end
end

local displayCounters = function()
  local redcoins   = memory.readbyte(REDCOINCOUNTER)
  local stars   = memory.readword(STARSCOUNTER) / 10 --?
  local flowers = memory.readbyte(FLOWERSCOUNTER)
  local coins = memory.readbyte(COINSCOUNTER)

  --gui.text( 1, 30, string.format("STARS   %d/30", stars)) 
  gui.text( 1, 30, string.format("COINS %d", coins))
  gui.text( 1, 38, string.format("REDCOINS %d/20", redcoins)) 
  gui.text( 1, 46, string.format("FLOWERS %d/5", flowers))  
end

-- Core
gui.register( function()  
  gui.transparency(guiTransparency)  
  if doShowInfo then
    if doShowCounters then displayCounters() end
    if doShowTimers then displayTimers() end
    if doShowYoshiInfo then displayYoshiInfo() end
  end    
end )
